<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Gio_Core_String
{
	private $_encoding = 'UTF-8';
	private $_escape = 'htmlspecialchars';
	
	private static $_instance = null;
	
	/**
	 * @return Gio_Core_Application
	 */
	public static function getInstance()
	{
		if (null == self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public function __construct()
	{
	}
	
	public function escape($var)
    {
        if (in_array($this->_escape, array('htmlspecialchars'))) {
            return call_user_func($this->_escape, $var, ENT_QUOTES, $this->_encoding);
        }
        return call_user_func($this->_escape, $var);
	}
	
	public function subString($string, $numWord = 50, $endString = '...')
	{
		$string = strip_tags($string);
		$words = explode(' ', $string);
		$numWord = (count($words) < $numWord) ? count($words) : $numWord;
		$stringReturn = null;
		for ($i = 0; $i < $numWord; $i++) {
			$stringReturn .= (null == $stringReturn) ? '' : ' ';
			$stringReturn .= $words[$i];
		}
		return $stringReturn . $endString;
	}
	
	public function isEmail($email)
	{
		return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
	}
	
	public function isUsername($username, $maxlength = 20)
	{
		if (!preg_match("!^[a-z0-9_~]{4," . $maxlength . "}$!i", $username)) {
        	return false;
		} 
		return true;
	}
	
	public static function removeSign($string, $seperator = '-', $allowANSIOnly = false) 
	{
		$pattern = array(
						"a" => "á|à|ạ|ả|ã|Á|À|Ạ|Ả|Ã|ă|ắ|ằ|ặ|ẳ|ẵ|Ă|Ắ|Ằ|Ặ|Ẳ|Ẵ|â|ấ|ầ|ậ|ẩ|ẫ|Â|Ấ|Ầ|Ậ|Ẩ|Ẫ",
						"o" => "ó|ò|ọ|ỏ|õ|Ó|Ò|Ọ|Ỏ|Õ|ô|ố|ồ|ộ|ổ|ỗ|Ô|Ố|Ồ|Ộ|Ổ|Ỗ|ơ|ớ|ờ|ợ|ở|ỡ|Ơ|Ớ|Ờ|Ợ|Ở|Ỡ",
						"e" => "é|è|ẹ|ẻ|ẽ|É|È|Ẹ|Ẻ|Ẽ|ê|ế|ề|ệ|ể|ễ|Ê|Ế|Ề|Ệ|Ể|Ễ",
						"u" => "ú|ù|ụ|ủ|ũ|Ú|Ù|Ụ|Ủ|Ũ|ư|ứ|ừ|ự|ử|ữ|Ư|Ứ|Ừ|Ự|Ử|Ữ",
						"i" => "í|ì|ị|ỉ|ĩ|Í|Ì|Ị|Ỉ|Ĩ",
						"y" => "ý|ỳ|ỵ|ỷ|ỹ|Ý|Ỳ|Ỵ|Ỷ|Ỹ",
						"d" => "đ|Đ",
						"c" => "ç",
					);
		while (list($key, $value) = each($pattern)) {
			$string = preg_replace('/' . $value . '/i', $key, $string);	
		}
		if ($allowANSIOnly) {
			$string = strtolower($string);
			$string = preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', $seperator, ''), $string);
		}
		return $string;
	}
	
	public static function removeSpecialChar($string)
	{
		$specialCharacters = array(
						'#' => '',
						'$' => '',
						'%' => '',
						'&' => '',
						'@' => '',
						'.' => '',
						'�' => '',
						'+' => '',
						'=' => '',
						'�' => '',
						'\\' => '',
						'/' => '',
		);
	 
		while (list($character, $replacement) = each($specialCharacters)) {
			$string = str_replace($character, $replacement, $string);
		}
		return $string;
	}
	
	/**
     * @param string $string
     * @param array $tags
     * @param bool $stripContent
     * @return string
     */
    public static function stripTags($string, $tags, $stripContent = false) 
    {
        $content = '';
        if (null == $tags) {
        	$string = preg_replace('#</?[^>]*>#is', '', $string);	
        } else {
	        if(!is_array($tags)) {
	            $tags = (strpos($string, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
	            if(end($tags) == '') {
	                array_pop($tags);        
	            }
	        }
	        foreach($tags as $tag) {
	            if ($stripContent) {
	                 $content = '(.+</'.$tag.'[^>]*>|)';
	            }
	            $string = preg_replace('#</?'.$tag.'[^>]*>'.$content.'#is', '', $string);
	        }
        }
        return trim($string);
    }  
    
	public static function stripCdata($string)
	{
    	preg_match_all('/<!\[CDATA\[(.*?)\]\]>/is', $string, $matches);
    	$string = str_replace($matches[0], $matches[1], $string);
    	$string = ltrim($string);
    	return rtrim($string);
	}
	
	public static function generateString($length = 10) {
	    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	    $length = ($length > strlen($characters)) ? strlen($characters) : $length;
	    $string = '';    
	    for ($i = 0; $i < $length; $i++) {
	        $string .= $characters[mt_rand(0, strlen($characters) - 1)];
	    }
    	return $string;
	}
}